package com.fuhe.chen.vendingmachine.controller.app;

import com.fuhe.chen.vendingmachine.constant.MessageConstant;
import com.fuhe.chen.vendingmachine.constant.WebConst;
import com.fuhe.chen.vendingmachine.dto.ApiResponse;
import com.fuhe.chen.vendingmachine.dto.ApiResponseForList;
import com.fuhe.chen.vendingmachine.dto.CommodityInBasket;
import com.fuhe.chen.vendingmachine.pojo.User;
import com.fuhe.chen.vendingmachine.service.IBasketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.fuhe.chen.vendingmachine.constant.MessageConstant.ShoppingCart.ADD_REPEAT;

@Controller
@ResponseBody
@RequestMapping("/app/basket")
@Api(tags = "购物车接口(面向app)")
public class BasketController {

    @Autowired
    IBasketService basketService;

    private static final Logger LOGGER = LoggerFactory.getLogger(BasketController.class);

    @ApiOperation(value = "增加商品信息到购物车")
    @PostMapping("/addCommodityToBasket")
    public ApiResponse<String> addCommodityToBasket(
            HttpServletRequest request,
            @RequestParam("commodity_id") Integer commodity_id,
            @RequestParam("commodity_specification_id") Integer commodity_specification_id,
            @RequestParam("count") Integer count,
            @RequestParam("user_id") String user_id
    ) {
        ApiResponse<String> response = new ApiResponse<>();

        User userInfo  = (User) request.getSession().getAttribute(WebConst.LOGIN_SESSION_KEY);
        if(!user_id.equals(userInfo.getId())){
            //非本账户操作,操作失败
            response.setCode(MessageConstant.Error.NO_ADD_PERMISSION_CODE);
            response.setMessage(MessageConstant.Error.NO_ADD_PERMISSION_MESSAGE);
            response.setSuccess(false);
            return response;
        }

        Integer result = basketService.addCommodityToBasket(commodity_id, commodity_specification_id, count, user_id);

        if(result.equals(ADD_REPEAT)){
            //添加商品信息重复
            response.setCode(ADD_REPEAT);
            response.setMessage(MessageConstant.ShoppingCart.COMMODITY_REPEAT);
            response.setSuccess(false);
            return response;
        }

        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.DO_SUCCESS_MESSAGE);
        response.setSuccess(true);
        return response;
    }

    @ApiOperation(value = "从购物车中删除商品信息")
    @PostMapping("/deleteCommodityFromBasket")
    public ApiResponse<String> deleteCommodityFromBasket(
            HttpServletRequest request,
            @RequestParam("basket_detail_id") Integer basket_detail_id,
            @RequestParam("user_id") String user_id
    ) {
        ApiResponse<String> response = new ApiResponse<>();

        User userInfo  = (User) request.getSession().getAttribute(WebConst.LOGIN_SESSION_KEY);
        if(!user_id.equals(userInfo.getId())){
            //非本账户操作,操作失败
            response.setCode(MessageConstant.Error.NO_DELETE_PERMISSION_CODE);
            response.setMessage(MessageConstant.Error.NO_DELETE_PERMISSION_MESSAGE);
            response.setSuccess(false);
            return response;
        }

        basketService.deleteCommodityFromBasket(basket_detail_id);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.DELETE_SUCCESS_MESSAGE);
        response.setSuccess(true);
        return response;
    }

    @ApiOperation(value = "根据用户id查询购物车信息")
    @GetMapping("/queryCommodityFromBasket/{user_id}")
    public ApiResponseForList<CommodityInBasket> queryCommodityFromBasket(
            HttpServletRequest request,
            @PathVariable("user_id") String user_id
    ) {
        ApiResponseForList<CommodityInBasket> response = new ApiResponseForList<>();

        User userInfo  = (User) request.getSession().getAttribute(WebConst.LOGIN_SESSION_KEY);
        if(!user_id.equals(userInfo.getId())){
            //非本账户操作,操作失败
            response.setCode(MessageConstant.Error.NO_QUERY_PERMISSION_CODE);
            response.setMessage(MessageConstant.Error.NO_QUERY_PERMISSION_MESSAGE);
            response.setSuccess(false);
            return response;
        }

        List<CommodityInBasket> goods = basketService.queryCommodityFromBasket(user_id);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setSuccess(true);
        response.setData(goods);
        return response;
    }
}
